package cn.wolfcode.luowowo.service.impl;

import cn.wolfcode.luowowo.article.domain.*;
import cn.wolfcode.luowowo.article.mapper.StrategyContentMapper;
import cn.wolfcode.luowowo.article.mapper.StrategyDetailMapper;
import cn.wolfcode.luowowo.article.query.StrategyDetailQuery;
import cn.wolfcode.luowowo.article.service.IDestinationService;
import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo.article.service.IStrategyService;
import cn.wolfcode.luowowo.article.service.IStrategyTagService;
import cn.wolfcode.luowowo.article.vo.DetailVO;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import java.util.Date;
import java.util.List;

@Service
public class StrategyDetailServiceImpl implements IStrategyDetailService {
    @Autowired
    private StrategyDetailMapper strategyDetailMapper;
    @Autowired
    private StrategyContentMapper strategyContentMapper;
    @Autowired
    private IStrategyService strategyService;
    @Autowired
    private IDestinationService destinationService;
    @Autowired
    private IStrategyTagService strategyTagService;

    public PageInfo query(StrategyDetailQuery qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        return new PageInfo(strategyDetailMapper.selectForList(qo));
    }

    public List<StrategyDetail> list() {
        return strategyDetailMapper.selectAll();
    }

    public StrategyDetail get(Long id) {
        //获取攻略明细对象的时候，把攻略内容也查询出来设置到攻略明细对象中
        StrategyContent content = strategyContentMapper.selectByPrimaryKey(id);
        StrategyDetail strategyDetail = strategyDetailMapper.selectByPrimaryKey(id);
        strategyDetail.setStrategyContent(content);
        return strategyDetail;
    }

    public void saveOrUpdate(StrategyDetail strategyDetail, String tags) {
        //dest_id，目的地id，根据大攻略的id拿到大攻略对象，再根据大攻略对象拿到目的地的id
        Strategy strategy = strategyService.get(strategyDetail.getStrategy().getId());
        Destination dest = strategy.getDest();
        strategyDetail.setDest(dest);
        //author_id，作者id，暂时不管
        //summary，摘要
        StrategyContent strategyContent = strategyDetail.getStrategyContent();
        String content = strategyContent.getContent();
        if(StringUtils.hasLength(content) && content.length() >= 200){
            strategyDetail.setSummary(content.substring(0,200));
        }else {
            strategyDetail.setSummary(content);
        }
        //creatTime，创建时间
        strategyDetail.setCreateTime(new Date());
        //isabord，是否是国外，找到土司的第一个，如果id是1则不是国外，否则就是国外
        List<Destination> toasts = destinationService.getToasts(dest.getId());
        strategyDetail.setIsabroad(toasts.get(0).getId() == 1? false: true);
        //新增或更新操作
        if (strategyDetail.getId() == null){
            strategyDetailMapper.insert(strategyDetail);
            //新增攻略内容
            strategyContent.setId(strategyDetail.getId());
            strategyContentMapper.insert(strategyContent);
        }else {
            strategyDetailMapper.updateByPrimaryKey(strategyDetail);
            //更新攻略内容
            strategyContent.setId(strategyDetail.getId());
            strategyContentMapper.updateByPrimaryKey(strategyContent);
        }
        //处理标签，先清空该攻略明细之前的标签
        strategyDetailMapper.deleteRelation(strategyDetail.getId());
        String[] tag = tags.split(",");
        for (String t : tag) {
            StrategyTag strategyTag = strategyTagService.getTagByName(t);
            if(strategyTag == null){    //如果该标签不存在，则新增到标签表中，并添加关系
                StrategyTag newTag = new StrategyTag();
                newTag.setName(t);
                newTag.setState(0);
                strategyTagService.saveOrUpdate(newTag);
                strategyDetailMapper.insertRelation(strategyDetail.getId(),newTag.getId());
            }else {     //如果该标签存在，则直接添加关系
                strategyDetailMapper.insertRelation(strategyDetail.getId(),strategyTag.getId());
            }
        }
    }

    public void changeState(Long id, int state) {
        strategyDetailMapper.changeState(id,state);
    }

    public List<StrategyDetail> getDetailsByCatalogId(Long catalogId) {
        List<StrategyDetail> details = strategyDetailMapper.selectByCatalogId(catalogId);
        for (StrategyDetail detail : details) {
            detail.setStrategyContent(strategyContentMapper.selectByPrimaryKey(detail.getId()));
        }
        return details;
    }

    public List<StrategyDetail> getViewnumTop3DetailsByDestId(Long destId) {
        return strategyDetailMapper.selectViewnumTop3DetailsByDestId(destId);
    }

    public List<StrategyDetail> getStrategyDetailsByIsAbroadTop10(boolean b) {
        return strategyDetailMapper.selectStrategyDetailsByIsAbroadTop10(b);
    }

    public int updateReplyNum(Long detailId, int replyNum) {
        int newReplyNum = strategyDetailMapper.selectByPrimaryKey(detailId).getReplynum() + 1;
        strategyDetailMapper.updateReplyNum(detailId,newReplyNum);
        return newReplyNum;
    }

    public void updateStatisByVo(DetailVO vo) {
        strategyDetailMapper.updateStatisByVo(vo);
    }
}